﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ExpenseSystem.Model;
using ExpenseSystem.Lor;
using ExpenseSystem.Common;
using ExpenseSystem.DataUpdater;
using ExpenseSystem.Fether;
using ExpenseSystem.Core.CustomerModules;
using ExpenseSystem.Core.DataAddOn;

namespace ExpenseSystem.Core.ServiceOrderModules
{
    public class ServiceOrderStateUpdateManager : ServiceOrderManager
    {
        int rows;
        ServiceOrderFetcher fetcher = new ServiceOrderFetcher();
        ServiceOrderDetailFetcher dFetcher = new ServiceOrderDetailFetcher();
        ServiceOrderManager orgServiceOrderManager;
        ServiceOrderDetailManager odManager = new ServiceOrderDetailBaseManager();
        public ServiceOrderStateUpdateManager(ServiceOrderManager ServiceOrderManager)
        {
            this.orgServiceOrderManager = ServiceOrderManager;
        }

        public override bool Delete(ServiceOrderModel ServiceOrder, out string message)
        {
            return orgServiceOrderManager.Delete(ServiceOrder, out message);
        }

        public override bool DeleteById(long[] ServiceOrderId, out string message)
        {
            return orgServiceOrderManager.DeleteById(ServiceOrderId, out message);
        }

        public override bool Save(ServiceOrderModel ServiceOrder, out string message, params IAddable<ServiceOrderModel>[] addables)
        {
            return orgServiceOrderManager.Save(ServiceOrder, out message, addables);
        }
        public override bool Approved(long id, out string message)
        {
            bool result = orgServiceOrderManager.Approved(id, out message);
            if (result)
            {
                IList<ServiceOrderDetailModel> models = dFetcher.ListByMainId(profile.SiteId, id, out message, out rows);
                foreach (ServiceOrderDetailModel model in models)
                {
                    model.State = Definition.STATE_APPROVED;
                    model.ApprovedDate = DateTime.Now.ToString(Definition.DATETIME_FORMAT);
                    model.Approver = profile.UserCode;
                    if (!odManager.Save(model, out message)) { return false; }
                }
            }
            return result;
        }
        public override bool Comfirm(long id, out string message)
        {
            bool result = orgServiceOrderManager.Comfirm(id, out message);
            if (result)
            {
                IList<ServiceOrderDetailModel> models = dFetcher.ListByMainId(profile.SiteId, id, out message, out rows);
                foreach (ServiceOrderDetailModel model in models)
                {
                    model.State = Definition.STATE_CONFIRM;
                    model.ConfirmedDate = DateTime.Now.ToString(Definition.DATETIME_FORMAT);
                    model.Confirmed = profile.UserCode;
                    if (!odManager.Save(model, out message)) { return false; }
                }
            }
            return result;
        }
        public override bool Recall(long modelid, out string message)
        {
            bool result = orgServiceOrderManager.Recall(modelid, out message);
            if (result)
            {
                IList<ServiceOrderDetailModel> models = dFetcher.ListByMainId(profile.SiteId, modelid, out message, out rows);
                foreach (ServiceOrderDetailModel model in models)
                {
                    model.State = Definition.STATE_NORMAL;
                    if (!odManager.Save(model, out message)) { return false; }
                }
            }
            return result;
        }
        public override bool Reject(long id, out string message)
        {
            bool result = orgServiceOrderManager.Reject(id, out message);
            if (result)
            {
                IList<ServiceOrderDetailModel> models = dFetcher.ListByMainId(profile.SiteId, id, out message, out rows);
                foreach (ServiceOrderDetailModel model in models)
                {
                    model.State = Definition.STATE_NORMAL;
                    model.ApprovedDate = DateTime.Now.ToString(Definition.DATETIME_FORMAT);
                    model.Approver = profile.UserCode;
                    if (!odManager.Save(model, out message)) { return false; }
                }
            }
            return result;
        }
        public override bool Revert(long modelid, out string message)
        {
            bool result = orgServiceOrderManager.Revert(modelid, out message);
            if (result)
            {
                IList<ServiceOrderDetailModel> models = dFetcher.ListByMainId(profile.SiteId, modelid, out message, out rows);
                foreach (ServiceOrderDetailModel model in models)
                {
                    model.State = Definition.STATE_NORMAL;
                    model.ApprovedDate = DateTime.Now.ToString(Definition.DATETIME_FORMAT);
                    model.Approver = profile.UserCode;
                    if (!odManager.Save(model, out message)) { return false; }
                }
            }
            return result;
        }
        public override bool Submit(long modelid, out string message)
        {
            bool result = orgServiceOrderManager.Submit(modelid, out message);
            if (result)
            {
                IList<ServiceOrderDetailModel> models = dFetcher.ListByMainId(profile.SiteId, modelid, out message, out rows);
                foreach (ServiceOrderDetailModel model in models)
                {
                    model.State = Definition.STATE_READY;
                    if (!odManager.Save(model, out message)) { return false; }
                }
            }
            return result;
        }
    }
}
